﻿///////////////////////////////////////////////////////////////////////////////
// 文件名： MeshRenderer.cc
// 作者：   671643387@qq.com
// 日期：   2015年11月1日 下午7:06:48
// 简介：   
///////////////////////////////////////////////////////////////////////////////

#include <memory.h>

#include "graphics/graphics/SceneNode.hpp"
#include "graphics/graphics/Scene.hpp"
#include "graphics/graphics/render/Render.hpp"

#include <graphics/graphics/component/Mesh.hpp>
#include "graphics/graphics/component/MeshRenderer.hpp"
using namespace ::graphics::graphics::component;

MeshRenderer::MeshRenderer()
    : ::graphics::Component("MeshRenderer")
    , buffer_()
{

}

MeshRenderer::~MeshRenderer()
{

}

///////////////////////////////////////////////////////////////////////////////

void MeshRenderer::Render()
{
    if (!CheckBuffer_())
        return;
}

///////////////////////////////////////////////////////////////////////////////

bool MeshRenderer::CheckBuffer_()
{
    ::graphics::ComponentPtr meshComp = parent_->GetComponent(std::string("Mesh"));
    if (!meshComp)
        return false;

    if(!meshComp->IsEnable())
        return false;

    ::graphics::graphics::component::Mesh* mesh 
        = static_cast<::graphics::graphics::component::Mesh*>(meshComp.get());

    if (!buffer_)
    {
        stProperties properties;

        properties.vp = mesh->GetVertices();
        properties.vpSize = mesh->GetVerticesSize();
        properties.ip = mesh->GetIndices();
        properties.ipSize = mesh->GetIndicesSize();
        properties.fvf = mesh->GetFVF();
        properties.indexType = mesh->GetIndexType();

        buffer_ = parent_->GetScene()->GetRender()->CreateBuffer(properties);
        if (buffer_)
        {
            return true;
        }

        return false;
    }
    else
    {
        if (mesh->IsDirty())
        {
            
        }
    }

    return true;
}